using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using NCT.Framework;
using NCT.Framework.Database;
using VISS.API.Commons;

namespace VISS.API.Users {
	class UserLivingProfileBC : IUserLivingProfile, IUserLivingProfileAdmin {

        #region IUserLivingProfile Members

        public bool Insert(UserLivingProfileEnt item) {
            bool result = true;
            DbConnection dbCon = null;
            DbTransaction dbTrans = null;
            try {
                //Get connection name
                string connectionName = ConfigInfo.ConnectionName;

                //Normalize data
                item.DateCreated = DateTime.Now;

                //Create connection & transaction
                dbCon = Database.CreateConnection(connectionName, true);
                dbTrans = dbCon.BeginTransaction();

                //Insert UserLivingProfile to DB
                result = UserLivingProfileDA.Insert(item, connectionName, dbCon, dbTrans) > 0;

                //Add log
                if (result) {
                }

                // Insert User into Cache
                if (ConfigInfo.UseCache && result) {

                }
            }
            catch (System.Exception ex) {
                LogUtils.Error(string.Format("VISS.Users.UserLivingProfileBC.Insert({0}): \n" + ex.ToString(), SerializerUtils.JsonSerialize(item)));
                result = false;
            }
            finally {
                if (dbTrans != null) {
                    if (result) {
                        dbTrans.Commit();
                    }
                    else {
                        // rollback
                        dbTrans.Rollback();
                    }
                }
                if (dbCon != null && dbCon.State == ConnectionState.Open) {
                    dbCon.Close();
                    
                }
            }
            return result;
        }

        public bool Update(UserLivingProfileEnt item) {
            bool result = true;
            DbConnection dbCon = null;
            DbTransaction dbTrans = null;
            try {
                //Get connection name
                string connectionName = ConfigInfo.ConnectionName;

                //Normalize data
                item.DateModified = DateTime.Now;

                //Create connection & transaction
                dbCon = Database.CreateConnection(connectionName, true);
                dbTrans = dbCon.BeginTransaction();

                //Update to DB
                result = UserLivingProfileDA.Update(item, connectionName, dbCon, dbTrans) > 0;

                //Add log
                if (result) {
                }

                // Update User into Cache
                if (ConfigInfo.UseCache && result) {
                }
            }
            catch (System.Exception ex) {
                LogUtils.Error(string.Format("VISS.Users.UserLivingProfileBC.Update({0}): \n" + ex.ToString(), SerializerUtils.JsonSerialize(item)));
                result = false;
            }
            finally {
                if (dbTrans != null) {
                    if (result) {
                        dbTrans.Commit();
                    }
                    else {
                        // rollback
                        dbTrans.Rollback();
                    }
                }
                if (dbCon != null && dbCon.State == ConnectionState.Open) {
                    dbCon.Close();
                    
                }
            }
            return result;
        }

        public UserLivingProfileEnt GetByUserKey(string userKey) {
            UserLivingProfileEnt item = null;
            try {
                //Get connection name
                string connectionName = ConfigInfo.ConnectionName;

                if (ConfigInfo.UseCache) {
                }
                else {

                    IPet _objPet = StructureMapFactory.GetInstance<IPet>();

                    item = UserLivingProfileDA.GetByUserKey(userKey, connectionName, null, null);
                    string[] petIDs = item.PetIDs.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                    for (int i = 0; i < petIDs.Length; i++) {
                        item.Pets += _objPet.GetByID(ConvertUtils.ToShort(petIDs[i])).Name + ",";
                    }
                }
            }
            catch (System.Exception ex) {
                LogUtils.Error(string.Format("VISS.Users.UserLivingProfileBC.GetByUserKey({0}): \n" + ex.ToString(), userKey));
            }
            return item;
        }

        #endregion
    }
}